<?php
/**
 * Created by PhpStorm.
 * User: j28981
 * Date: 2015/8/24
 * Time: 10:26
 * 对商品表的操作控制器
 */
class goodsAction extends Action{
    private $goods_fields = array('good_id'=>'商品id','good_name'=>'商品名称',/*'cate_id'=>'商品分类'*/);
    /**
     *列表 ：分页显示、查询功能
     */
    public function goods_listOp(){
        $search_field = isset($_POST['search_field'])?$_POST['search_field']:'';
        $field_value = isset($_POST['field_value'])?$_POST['field_value']:'';
        $goods_model = new goodsModel();
        /*分页 S*/
        $page_length = 3;
        $goods_count = $goods_model->getGoodsCount();
        $page_obj = new Page("index.php?act=goods&op=goods_list",$page_length,$goods_count);
        $page_str = $page_obj->pageString();
        $goods_list = $goods_model->getAllGoods($page_obj->limit_offset,$page_length,$search_field,$field_value);
        $goods_fields = $this->goods_fields;
        /*分页 E*/
        include TEMPLATES_DIR.DS."goods_list.php";
    }
    public function goods_collectOp(){
        $search_field = isset($_POST['search_field'])?$_POST['search_field']:'';
        $field_value = isset($_POST['field_value'])?$_POST['field_value']:'';
        $goods_model = new goodsModel();
        /*分页 S*/
        $page_length =2;
        $goods_count = $goods_model->getGoodsCount(0);
        $page_obj = new Page("index.php?act=goods&op=goods_collect",$page_length,$goods_count);
        $page_str = $page_obj->pageString();
        $goods_list = $goods_model->getAllGoods($page_obj->limit_offset,$page_length,$search_field,$field_value,0);
        $goods_fields = $this->goods_fields;
        /*分页 E*/
        include TEMPLATES_DIR.DS."goods_collect.php";
    }
    //更改商品状态为1
    public function goods_restoreOp(){
        $goods_id = $_GET['goods_id'];
        $goods_model= new goodsModel();
        if($goods_model->goodsRestore($goods_id)){
            $this->showMessage('商品已还原到商品列表','index.php?act=goods&op=goods_collect',1);
        }else{
            $this->showMessage('商品还原失败','index.php?act=goods&op=goods_collect');
        }
    }

    /**
     *永久删除商品：如果上传了图片 ，同时将图片删除
     */
    public function goods_del_realOp(){
        $goods_id = $_GET['goods_id'];
        $goods_model= new goodsModel();
        $goods_array = $goods_model->getGoodsById($goods_id);
        $oriimgs=array();
        $oriimgs['oriimg_path'] = SITE_ROOT.DS.$goods_array['good_oriimg'];
        $oriimgs['thumb_path'] = SITE_ROOT.DS.$goods_array['good_thumb'];
        $oriimgs['water_path'] = SITE_ROOT.DS.$goods_array['good_water'];
        if(!empty($goods_array['good_oriimg']) || !empty($goods_array['good_thumb']) || !empty($goods_array['good_water'])){
            foreach($oriimgs as $v){
                @unlink($v);
            }
        }
        if($goods_model->delGoodsReal($goods_id)){
            $this->showMessage('商品已删除','index.php?act=goods&op=goods_collect',1);
        }else{
            $this->showMessage('商品删除失败','index.php?act=goods&op=goods_collect');
        }
    }

    /**
     * 2步
     *添加要查询分类表，上传商品单图片
     */
    public function goods_addOp(){
        if(isset($_POST['submit_goods'])){
            $insert_array = array();
            $insert_array['cate_id'] = $_POST['cate_id'];
            $insert_array['good_name'] = trim($_POST['goods_name']);
             $insert_array['good_price'] = trim($_POST['goods_price']);
             $insert_array['good_oriimg'] = "";
             $insert_array['good_thumb'] = "";
             $insert_array['good_water'] = "";
             $insert_array['good_desc'] = trim($_POST['goods_desc']);
           // var_dump($_FILES['goods_img']);
            if(!empty($_FILES['goods_img']['name'])){
                /*如果添加了图片，则进行图片上传、缩略图、水印图操作*/
                $upload_obj = new Upload('goods_img','goods',1);
                $goods_oriimg_url = $upload_obj->uploadOne();
                $insert_array['good_oriimg'] = $goods_oriimg_url;
                $image_thumb_obj = new Image_thumb($goods_oriimg_url);
                $insert_array['good_thumb'] = $image_thumb_obj->makeThumb();
                $image_water_obj = new Image_water($goods_oriimg_url);
                $insert_array['good_water'] = $image_water_obj->makeWater();
            }
            $goods_model = new goodsModel();
            if($goods_model->addGoods($insert_array)){
                $this->showMessage('添加商品成功','index.php?act=goods&op=goods_list',1);
            }else{
                echo "goods-add-fail";
            }

        }else{
           // $cate_model = new cateModel();
           // $cate_list =  $cate_model->getCateLevel0();
            include TEMPLATES_DIR.DS."goods_add.php";
        }

    }

    /**
     *编辑注意ID接收和传递，图片如果编辑了，重新上传新图，删除旧图，没有编辑当然使用原图
     */
    public function goods_editOp(){
        if(isset($_POST['submit_goods_edit'])){
            $goods_id = isset($_POST['goods_id'])?(int)$_POST['goods_id']:0;
            if(!$goods_id){
                $this->showMessage('请提交要编辑的商品id','index.php?act=goods&op=goods_list');
            }
            $cate_id = isset($_POST['cate_id'])?$_POST['cate_id']:-1;

            $update_array = array();
            if($cate_id!=-1){
                $update_array['cate_id'] = $cate_id;
            }
            $update_array['good_name'] = trim($_POST['goods_name']);
            $update_array['good_price'] = trim($_POST['goods_price']);
            $update_array['good_desc'] = trim($_POST['goods_desc']);
            if(!empty($_FILES['goods_img']['name'])){
                /*表示上传了新图，进行一系列图片处理，并删除原来的图*/
                //上传新图-》缩略图-》水印图
                $upload_obj = new Upload('goods_img','goods',1);
                $goods_oriimg_url = $upload_obj->uploadOne();
                $update_array['good_oriimg'] = $goods_oriimg_url;
                $image_thumb_obj = new Image_thumb($goods_oriimg_url);
                $update_array['good_thumb'] = $image_thumb_obj->makeThumb();
                $image_water_obj = new Image_water($goods_oriimg_url);
                $update_array['good_water'] = $image_water_obj->makeWater();
                //查找并删除此商品原来的三张图，如果有的话
                $goods_model = new goodsModel();
                $goods_array = $goods_model->getGoodsById($goods_id);
                $oriimgs=array();
                $oriimgs['oriimg_path'] = SITE_ROOT.DS.$goods_array['good_oriimg'];
                $oriimgs['thumb_path'] = SITE_ROOT.DS.$goods_array['good_thumb'];
                $oriimgs['water_path'] = SITE_ROOT.DS.$goods_array['good_water'];
                if(!empty($goods_array['good_oriimg']) || !empty($goods_array['good_thumb']) || !empty($goods_array['good_water'])){
                    foreach($oriimgs as $v){
                        @unlink($v);
                    }
                }
            }
            $goods_model = new goodsModel();
            if($goods_model->updateGoodsById($goods_id,$update_array)){
                $this->showMessage('商品编辑成功','index.php?act=goods&op=goods_list',1);
            }else{
                $this->showMessage('没有进行修改或修改失败',"index.php?act=goods&op=goods_edit&goods_id=$goods_id");
            }

        }else{
            $goods_id = isset($_GET['goods_id'])?(int)$_GET['goods_id']:0;
            if(!$goods_id){
                $this->showMessage('请提交要编辑的商品id','index.php?act=goods&op=goods_list');
            }
            $goods_model = new goodsModel();
            $goods_array = $goods_model->getGoodsById($goods_id);
            $cate_model = new cateModel();
            $cate_list =  $cate_model->getCateLevel0();
            include_once TEMPLATES_DIR.DS."goods_edit.php";
        }
    }

    /**
     *和编辑一样，也要注意接收ID，商品删除可以只是改变一种状态，不真正删除
     */
    public function goods_del_oneOp(){
        $goods_id = isset($_GET['goods_id'])?(int)$_GET['goods_id']:0;
        $goods_model = new goodsModel();
        $res = $goods_model->delteGoodsOne($goods_id);
        if($res['status']){
            $this->showMessage($res['msg'],'index.php?act=goods&op=goods_list',1);
        }else{
            $this->showMessage($res['msg'],'index.php?act=goods&op=goods_list');
        }
    }

    /**
     *批量删除商品
     */
    public function goods_del_moreOp(){
        $goods_id_array = isset($_POST['del'])?$_POST['del']:'';
        //var_dump($goods_id_array);die;
        $goods_model = new goodsModel();
        $res = $goods_model->delGoodsMore($goods_id_array);
        if($res['status']){
            $this->showMessage($res['msg'],'index.php?act=goods&op=goods_list',1);
        }else{
            $this->showMessage($res['msg'],'index.php?act=goods&op=goods_list');
        }
    }
    public function liuyanOp(){
        $lyfile = SITE_ROOT.DS.'Lyan/liuyan.txt';
            if(isset($_POST['liuyan'])){
                $liuyantxt = $_POST['liuyantxt']."<br />";
                file_put_contents($lyfile,$liuyantxt,FILE_APPEND);
            }
        $liuyans = file_get_contents($lyfile);
        $liuyan_array = explode('<br />',$liuyans);
        unset($liuyan_array[count($liuyan_array)-1]);
        include_once TEMPLATES_DIR.DS."liuyan.html";
    }
}